package com.hongye.util;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 请求环信接口
 * @author liushaomin
 */
public class HuanXinUtils{
	
	private static Logger log = Logger.getLogger(HuanXinUtils.class);
	public static final String CLIENT_ID = "YXA6oaKKUI7OEea4w4v1GOfkkA";
	public static final String CLIENT_SECRET  = "YXA66Hy6tJpIVHQpilGU1YXCq1QWQq8";
	public static final String ORG_NAME  = "1112161010115754";
	public static final String APP_NAME  = "rebogou";
	public static final String BASE_URL ="https://a1.easemob.com/" + ORG_NAME + "/" + APP_NAME ;

	/**
	 * 获取token
	 * url = base_url/org_name/app_name/token
	 * @author liushaomin
	 * "expires_in" token的有效时间，需要判断超过有效时间了，自动获取
	 * 2016年10月10日
	 * @return
	 * @throws JSONException 
	 */
	public static String getToken(){
		Map<String, String> Params = new HashMap<String, String>();
		Params.put("grant_type", "client_credentials");
		Params.put("client_id", CLIENT_ID);
		Params.put("client_secret", CLIENT_SECRET);
		String url = BASE_URL + "/token";
		String result = HttpUtils.get(HttpUtils.Parameter(url, Params));
		Map<String, Object> map = Utils.toMap(result);
		String token = map.get("access_token").toString();
		if(log.isInfoEnabled()){log.info("getToken result="+result);}
		return token;
	}
	
	/**
	 * 添加环信用户  授权注册
	 * @author liushaomin
	 * 2016年10月12日
	 * @return
	 */
	public static String addUsers(String username, String password, String nickname){
		String urlstr = BASE_URL + "/users";
		String result = null;
		JSONObject params = new JSONObject();
		try {
			params.put("username", username);
			params.put("password", password);
			params.put("nickname", nickname);
			result = HttpUtils.HttpURL(urlstr, params, "POST", "Bearer " + getToken());
			if(log.isInfoEnabled()){log.info("addUsers result="+result);}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 获取单个用户信息
	 * @author liushaomin
	 * 2016年10月12日
	 * @param username
	 * @return 返回结果 json
	 *  {"action" : "get","path" : "/users","uri" : "http://a1.easemob.com/1112161010115754/rebogou/users/15031554160","entities" : 
	 *  [ {"uuid" : "cf33fb7a-902c-11e6-90b4-1925639c96ce","type" : "user","created" : 1476243281063,"modified" : 1476243281063,
	 *  "username" : "15031554160","activated" : true,"nickname" : "nick"} ],"timestamp" : 1476252575683, "duration" : 6,"count" : 1}
	 */
	public static String getSingleUser(String username){
		String urlstr = BASE_URL + "/users/" + username;
		String result = HttpUtils.get(urlstr, HuanXinUtils.getToken());
		if(log.isInfoEnabled()){log.info("getSingleUser result="+result);}
		return result;
	}
	
	/**
	 * 重置密码
	 * @author liushaomin
	 * 2016年10月12日
	 * @param username
	 * @param newpassword
	 * @return json {"action":"set user password","timestamp":1476256284656,"duration":13}
	 */
	public static String resetPassword(String username, String newpassword){
		String urlstr = BASE_URL + "/users/" + username + "/password";
		JSONObject params = new JSONObject();
		String result = null;
		try {
			params.put("newpassword", newpassword);
			result = HttpUtils.HttpURL(urlstr, params, "PUT", "Bearer " + getToken());
			if(log.isInfoEnabled()){log.info("ResetPassword result="+result);}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 修改用户昵称
	 * @author lifuqiang
	 * 2016-10-12
	 * @param username
	 * @param nickname
	 * @return json {"action" : "put",  "application" : "a1a28a50-8ece-11e6-b8c3-8bf518e7e490",  "path" : "/users",  
	 * "uri" : "https://a1.easemob.com/1112161010115754/rebogou/users",  "entities" : [ {"uuid" : "a67c4dd0-904b-11e6-8250-d5940c5e92ff",
	 * "type" : "user",    "created" : 1476256527149,    "modified" : 1476259308431,    "username" : "15933675785",   
	 * "activated" : true,    "nickname" : "abcd"  } ],  "timestamp" : 1476259308425,  "duration" : 22,  "organization" : 
	 * "1112161010115754",  "applicationName" : "rebogou"}
	 */
	public static String resetNickName(String username, String nickname ){
		String urlstr = BASE_URL + "/users/" + username;
		JSONObject params = new JSONObject();
		String result = null;
		try {
			params.put("nickname", nickname);
			result = HttpUtils.HttpURL(urlstr, params, "PUT", "Bearer " + getToken());
			if(log.isInfoEnabled()){log.info("ResetNickName result="+result);}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 获取用户在线状态（"stliu":"fline"：离线，"stliu": "online"：在线）
	 * @author lifuqiang
	 * 2016-10-12
	 * @param username
	 * @return json {"action" : "get","uri" : "http://a1.easemob.com/1112161010115754/rebogou/users/15933675785/status",
	 * "entities" : [ ], "data" : { "15933675785" : "offline"}, "timestamp" : 1476261623670,"duration" : 11, "count" : 0}
	 */
	public static String getUserStatus(String username){
		String urlstr = BASE_URL + "/users/" + username + "/status";
		String result = HttpUtils.get(urlstr, HuanXinUtils.getToken());
		if(log.isInfoEnabled()){log.info("getUserStatus result="+result);}
		return result;
	}
}
